package org.fuxia.im.core.server;

import java.util.concurrent.ConcurrentHashMap;

import org.fuxia.im.core.AbstractContext;
import org.fuxia.im.core.iconst.ServerConst;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * 服务器端上下文类，存放服务器一些公用数据信息
 *
 */
public class ServerSideContext extends AbstractContext
{

	private static ServerSideContext	instance;
	private static byte[]				lock	= new byte[1];

	private ServerSideContext()
	{
		context = new ConcurrentHashMap<Object, Object>();
		put(ServerConst.APPLICATION_CONTEXT, new ClassPathXmlApplicationContext("applicationContext.xml"));
	}

	public static ServerSideContext getContext()
	{
		synchronized (lock)
		{
			if (instance == null)
			{
				instance = new ServerSideContext();
			}
		}
		return instance;
	}

	public <T> T getBean(Class<T> clazz)
	{
		return getSpringContext().getBean(clazz);
	}

	public ApplicationContext getSpringContext()
	{
		Object appContext = context.get(ServerConst.APPLICATION_CONTEXT);
		if (appContext != null)
		{
			return (ClassPathXmlApplicationContext) appContext;
		}
		return null;
	}
}
